import { Component } from '@angular/core';
import { IonicPage,NavController,ToastController,Platform  } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
import { Network } from '@ionic-native/network';
import { Camera } from '@ionic-native/camera';
import { AddressApi } from '../../api/login/AddressApi';
import { Geolocation } from '@ionic-native/geolocation';

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private geo:Geolocation, private address:AddressApi, private platform:Platform, private camera:Camera, public navCtrl:NavController, private barcodeScanner:BarcodeScanner, private toastCtrl:ToastController, private network:Network) {

  }

  ngAfterViewInit() {
    this.showMsg(this.network.type);
  }

  scan() {
    this.barcodeScanner.scan().then((data) => {
      this.showMsg(data.text);
    }, (err)=> {
      this.showMsg("扫描出现异常" + err);
    });
  }

  lookNetwork() {

    var options = {
      enableHighAccuracy: true,  // 是否使用 GPS
      maximumAge: 30000,         // 缓存时间
      timeout: 27000,            // 超时时间
      coorType: 'bd09ll'         // 默认是 gcj02，可填 bd09ll 以获取百度经纬度用于访问百度 API
    }
    navigator.geolocation.getCurrentPosition((position:any) =>{
      this.address.getAddress(<string>position.coords.latitude,<string>position.coords.longitude).subscribe(res => {
        this.showMsg(JSON.stringify(res));
      });
    },(error:any) => {
      this.showMsg(error);
    },options);
  }

  showMsg(msg:string) {
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  }

  toForcedPhoto(){
    this.navCtrl.push("RuleScan", {
      "positionId": 0
    });
  }
  testdate(){
   // console.log(this.myFormat('yyyy-MM-dd hh:mm:ss',new Date));
    console.log(new Date());
  }

  myFormat(fmt:string,data:Date) {
    var o = {
      "M+": data.getMonth() + 1,                 //月份
      "d+": data.getDate(),                    //日
      "h+": data.getHours(),                   //小时
      "m+": data.getMinutes(),                 //分
      "s+": data.getSeconds(),                 //秒
      "q+": Math.floor((data.getMonth() + 3) / 3), //季度
      "S": data.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (data.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }


}
